/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	"github.com/valyala/fasthttp"
	"saas_sys_base/common"

	. "saas_sys_base/modules/adminSaas/models"
	. "saas_sys_base/modules/adminSaas/service"
)

type AdminSaasRoleCtrl struct {
	service     AdminSaasRoleService
	homeService AdminSaasHomeService
}

func init() {
	vc := new(AdminSaasRoleCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *AdminSaasRoleCtrl) CtrlInfo() string {
	return "AdminSaasRoleCtrl"
}

func (vc *AdminSaasRoleCtrl) RoutInit() {
	// 列表页面
	lego.GET("/adminSaas/AdminSaasRole/listpage", vc.ListPage, lego.LEGO_SESSION_TYPE_ADMIN, false)
	lego.GET("/adminSaas/AdminSaasRole/listjson", vc.ListJson, lego.LEGO_SESSION_TYPE_ADMIN, false)
	// 编辑页面
	lego.GET("/adminSaas/AdminSaasRole/editpage", vc.GetEditPage, lego.LEGO_SESSION_TYPE_ADMIN, false)
	lego.POST("/adminSaas/AdminSaasRole/editpage", vc.PostEditPage, lego.LEGO_SESSION_TYPE_ADMIN, false)
	// 删除
	lego.POST("/adminSaas/AdminSaasRole/del", vc.DelRecode, lego.LEGO_SESSION_TYPE_ADMIN, false)
}

/* 列表接口
* params:
  ---
*/
func (vc *AdminSaasRoleCtrl) ListJson(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := ""
	for {
		// 表单校验
		type ReqParams struct {
			Keyword string // 关键字查询
			Page    int    `vd:"$>=0"` // 分页
			PSize   int    `vd:"$>0"`  // 分页大小
		}
		req := ReqParams{
			Keyword: string(ctxExt.Peek("keyword")),
			Page:    utils.BytesToInt(ctxExt.Peek("page")),
			PSize:   utils.BytesToInt(ctxExt.Peek("limit")),
		}
		if err := lego.Validate(req); err != nil {
			code = 100
			msg = err.Error()
			break
		}

		// 业务查询
		if list, total, err := vc.service.ListAdminSaasRole(req.Keyword, req.Page, req.PSize); err != nil {
			lego.LogError(err)
		} else {
			response["count"] = total
			response["data"] = list
		}
		break
	}

	response["code"] = code
	response["msg"] = msg
	ctxExt.JSON(200, response)
	return
}

/* 添加数据记录
* params:
  ---
*/
func (vc *AdminSaasRoleCtrl) ListPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	data := make(map[string]interface{})
	for {
		break
	}
	ctxExt.HTML("system/view/AdminSaasRole/list.html", data)
	return
}

/* 添加数据记录
* params:
  ---
*/
func (vc *AdminSaasRoleCtrl) GetEditPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	data := make(map[string]interface{})
	for {
		type ReqParams struct {
			Id int64 // 记录id，为空则新建，否则为编辑
		}
		req := ReqParams{
			Id: utils.BytesToInt64(ctxExt.Peek("id")),
		}
		if err := lego.Validate(req); err != nil {
			break
		}

		target := vc.service.GetAdminSaasRoleById(req.Id)
		data["detail"] = target
		who := common.NewWho(0, 0, 0) //注意：目前版本admin saas不需要权限角色检查，暂时先让她编译通过
		menus := vc.homeService.GetSystemMenu(who, common.MenuMode_Tenant)
		data["menus"] = menus

		// 角色对应的权限信息
		if nil != target {
			tbroleright := AdminSaasRoleRight{}
			roleright := tbroleright.ListByRoleId(target.Id)

			checkfun := func(mid int64) bool {
				for _, r := range roleright {
					if r.SId == mid {
						return true
					}
				}
				return false
			}

			checkfun = checkfun

			for i, _ := range menus {
				menus[i].Check = checkfun(menus[i].Id)
				for j, _ := range menus[i].SubMenus {
					menus[i].SubMenus[j].Check = checkfun(menus[i].SubMenus[j].Id)
				}
			}
		}

		break
	}
	ctxExt.HTML("system/view/AdminSaasRole/edit.html", data)
	return
}

/* 数据编辑
* params:
  ---
*/
func (vc *AdminSaasRoleCtrl) PostEditPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	data := make(map[string]interface{})

	code := 0
	msg := ""

	for {
		// 表单校验
		type ReqParams struct {
			Id int64 `vd:""`
			//Cdate string `vd:""`
			//Udate string `vd:""`
			Name  string `vd:""`
			Brief string `vd:""`
			Menus string `vd:""`
		}
		req := ReqParams{
			Id: utils.BytesToInt64(ctxExt.Peek("id")),
			//Cdate: string(ctxExt.Peek("cdate")),
			//Udate: string(ctxExt.Peek("udate")),
			Name:  string(ctxExt.Peek("name")),
			Brief: string(ctxExt.Peek("brief")),
			Menus: string(ctxExt.Peek("mi")),
		}
		if err := lego.Validate(req); err != nil {
			code = 100
			msg = err.Error()
			break
		}

		// 业务处理
		recode := vc.service.GetAdminSaasRoleById(req.Id)
		if recode == nil {
			// 新建
			recode := new(AdminSaasRole)
			recode.Id = req.Id
			//recode.Cdate = req.Cdate
			//recode.Udate = req.Udate
			recode.Name = req.Name
			recode.Brief = req.Brief

			if false == vc.service.SaveAdminSaasRole(recode, req.Menus) {
				code = 100
				msg = "false"
				break
			}
		} else {
			recode.Id = req.Id
			//recode.Cdate = req.Cdate
			//recode.Udate = req.Udate
			recode.Name = req.Name
			recode.Brief = req.Brief

			if false == vc.service.UpdateAdminSaasRole(recode, req.Menus) {
				code = 100
				msg = "false"
				break
			}
		}
		break
	}
	response["code"] = code
	response["msg"] = msg
	response["data"] = data
	ctxExt.JSON(200, response)
	return
}

/* 删除数据记录
* params:
  ---
*/
func (vc *AdminSaasRoleCtrl) DelRecode(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	data := make(map[string]interface{})

	code := 0
	msg := ""
	for {
		// 表单校验
		type ReqParams struct {
			Id int64 `vd:"$>0"` // 记录id
		}
		req := ReqParams{
			Id: utils.BytesToInt64(ctxExt.Peek("id")),
		}
		if err := lego.Validate(req); err != nil {
			break
		}
		// 业务删除
		target := vc.service.GetAdminSaasRoleById(req.Id)
		if target == nil {
			code = 100
			msg = "记录不存在"
			break
		}

		if false == vc.service.DelAdminSaasRoleById(target.Id) {
			code = 100
			msg = "操作失败"
			break
		}
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = data
	ctxExt.JSON(200, response)
	return
}
